﻿using Absky.Data.Repository;
using Absky.Entity.LanguageModule;
using Absky.Model.Param.LanguageModule;
using Absky.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Absky.Service.LanguageModule
{
    /// <summary>
    /// 语言映照
    /// </summary>
    public class LanguageMapService : RepositoryFactory
    {
        #region 获取数据
        public async Task<List<LanguageMapEntity>> GetList(LanguageMapListParam param)
        {
            var expression = ListFilter(param);
            var list = await this.BaseRepository().FindList(expression);
            return list.ToList();
        }

        public async Task<LanguageMapEntity> GetEntity(long id)
        {
            return await this.BaseRepository().FindEntity<LanguageMapEntity>(id);
        }
        #endregion

        #region 提交数据
        public async Task SaveForm(LanguageMapEntity entity)
        {
            if (entity.Id.IsNullOrZero())
            {
                await entity.Create();
                await this.BaseRepository().Insert(entity);
            }
            else
            {
                await entity.Modify();
                await this.BaseRepository().Update(entity);
            }
        }

        public async Task DeleteForm(string ids)
        {
            var db = await this.BaseRepository().BeginTrans();
            try
            {
                long[] idArr = TextHelper.SplitToArray<long>(ids, ',');
                await db.Delete<LanguageMapEntity>(idArr);
                await db.CommitTrans();
            }
            catch
            {
                await db.RollbackTrans();
                throw;
            }
        }
        #endregion

        #region 私有方法
        private Expression<Func<LanguageMapEntity, bool>> ListFilter(LanguageMapListParam param)
        {
            var expression = LinqExtensions.True<LanguageMapEntity>();
            if (param != null)
            {
                if (!param.Code.IsEmpty())
                {
                    expression = expression.And(t => t.Code.Contains(param.Code));
                }
            }
            return expression;
        }
        #endregion
    }
}
